package com.nhjf.dao.teachers;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.nhjf.dao.BaseDao;
import com.nhjf.model.teachers.ReviewPainting;
import com.nhjf.pojo.qo.teachers.ReviewPaintingQo;
@Repository
public class ReviewPaintingDao extends BaseDao<ReviewPainting, ReviewPaintingQo>{

	@Override
	protected Criteria buildCriteria(Criteria criteria, ReviewPaintingQo qo) {
		// TODO Auto-generated method stub
		if(qo!=null) {

			criteria.setFetchMode("user", qo.isSeachUser()?FetchMode.JOIN:FetchMode.SELECT);	
			criteria.setFetchMode("teacher", qo.isSeachTeacher()?FetchMode.JOIN:FetchMode.SELECT);	
			if(qo.getTeacherId()!=null) {
				criteria.add(Restrictions.eq("teacher.id",qo.getTeacherId()));
			}

			if(qo.getUserId()!=null) {
				criteria.add(Restrictions.eq("user.id",qo.getUserId()));
			}
			if(StringUtils.isNotBlank(qo.getNickname())) {
				criteria.createAlias("student", "student");
				criteria.add(Restrictions.like("student.nickname",qo.getNickname(),MatchMode.ANYWHERE));
			}
			if(qo.getSccore()!=null) {
				criteria.add(Restrictions.ge("sccore",qo.getSccore()));
			}
			if(StringUtils.isNotBlank(qo.getIntroduce())) {
				criteria.add(Restrictions.like("introduce",qo.getIntroduce(),MatchMode.ANYWHERE));
			}
			if(qo.getStatus()!=null) {
				criteria.add(Restrictions.eq("status",qo.getStatus()));
			}
			if(qo.getIsOrganizationPainting()!=null) {
				criteria.add(Restrictions.eq("isOrganizationPainting",qo.getIsOrganizationPainting()));
			}
			if(qo.getTypeOne()!=null) {
				criteria.add(Restrictions.eq("typeOne",qo.getTypeOne()));
			}
			if(qo.getTypeTwo()!=null) {
				criteria.add(Restrictions.eq("typeTwo",qo.getTypeTwo()));
			}
			if(qo.getWidth()!=null) {
				criteria.add(Restrictions.eq("width",qo.getWidth()));
			}
			if(qo.getHeight()!=null) {
				criteria.add(Restrictions.eq("height",qo.getHeight()));
			}
			if(StringUtils.isNotBlank(qo.getChangepaintingURL())) {
				criteria.add(Restrictions.eq("changepaintingURL",qo.getChangepaintingURL()));
			}
			if(StringUtils.isNotBlank(qo.getNotchangedURL())) {
				criteria.add(Restrictions.eq("notchangedURL",qo.getNotchangedURL()));
			}
			if(StringUtils.isNotBlank(qo.getTotalcommentsound())) {
				criteria.add(Restrictions.eq("totalcommentsound",qo.getTotalcommentsound()));
			}
			if(qo.getReleasetime()!=null) {
				criteria.add(Restrictions.ge("releasetime",qo.getReleasetime()));
			}
//			if(qo.getReviewtime()!=null) {
//				criteria.add(Restrictions.ge("reviewtime",qo.getReviewtime()));
//			}
			if(qo.isSccoreSort()) {
				criteria.addOrder(Order.desc("sccore"));	
			}
				
			if(qo.isChangepaintingSort()) {
				criteria.addOrder(Order.desc("reviewtime"));	
			}
			if(qo.getReviewId()!=null) {
				criteria.add(Restrictions.eq("id",qo.getReviewId()));
			}
			if(qo.getReviewStartingTime()!=null) {
				criteria.add(Restrictions.ge("reviewtime",qo.getReviewStartingTime()));
			}
			if(qo.getReviewEndTime()!=null) {
				criteria.add(Restrictions.lt("reviewtime",qo.getReviewEndTime()));
			}
			if(qo.getSccoreType()!=null) {
				if(qo.getSccoreType()==1) {
					criteria.add(Restrictions.eq("status",2));
					criteria.add(Restrictions.ge("sccore",80));
				}else if(qo.getSccoreType()==2) {
					criteria.add(Restrictions.eq("status",2));
					criteria.add(Restrictions.ge("sccore",70) );
					criteria.add(Restrictions.lt("sccore",80) );
				}else if(qo.getSccoreType()==3) {
					criteria.add(Restrictions.eq("status",2));
					criteria.add(Restrictions.ge("sccore",60));
					criteria.add(Restrictions.lt("sccore",70));
				}else if(qo.getSccoreType()==4) {
					criteria.add(Restrictions.eq("status",2));
				}
			}
			
			
		}
		return criteria;
	}

	@Override
	protected Class<ReviewPainting> getEntityClass() {
		// TODO Auto-generated method stub
		return ReviewPainting.class;
	}

}
